/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShoppingCartCard.java
 *
 * Created on Mar 2, 2010, 8:27:23 PM
 */

package dat215projekt.gui.components;

import dat215projekt.data.FavoriteListener;
import dat215projekt.data.IMatDataHandlerExtended;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.List;
import javax.swing.BoxLayout;
import se.chalmers.cse.dat215.project.ShoppingCartListener;
import se.chalmers.cse.dat215.project.ShoppingItem;

/**
 *
 * @author teerik
 */
public class ShoppingCartCard extends javax.swing.JPanel implements ShoppingCartListener, FavoriteListener{
    private static final NumberFormat moneyFormat;
    private static final IMatDataHandlerExtended handler = IMatDataHandlerExtended.getInstance();
    private Navigation nav;

    static {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        symbols.setGroupingSeparator(' ');

        moneyFormat = new DecimalFormat("###,##0.00 kr",symbols);
    }
    
    public void setNavigationRef(Navigation nav){
        this.nav = nav;
    }

    /** Creates new form ShoppingCartCard */
    public ShoppingCartCard() {
        initComponents();

        handler.addFavoriteListener(this);
        handler.addShoppingCartListener(this);

        jScrollPane1.getVerticalScrollBar().setUnitIncrement(40);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        amountLabel = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        numProducts = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        goToCheckOut = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        productsPanel = new javax.swing.JPanel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(dat215projekt.Dat215projektApp.class).getContext().getResourceMap(ShoppingCartCard.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setOpaque(false);

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jPanel1.add(jLabel1);

        jPanel3.setBackground(resourceMap.getColor("jPanel3.background")); // NOI18N
        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setOpaque(false);

        jPanel5.setBackground(resourceMap.getColor("jPanel5.background")); // NOI18N
        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setOpaque(false);

        amountLabel.setFont(resourceMap.getFont("jLabel25.font")); // NOI18N
        amountLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        amountLabel.setText(resourceMap.getString("amountLabel.text")); // NOI18N
        amountLabel.setName("amountLabel"); // NOI18N

        jLabel27.setFont(resourceMap.getFont("jLabel25.font")); // NOI18N
        jLabel27.setText(resourceMap.getString("jLabel27.text")); // NOI18N
        jLabel27.setName("jLabel27"); // NOI18N

        numProducts.setFont(resourceMap.getFont("jLabel25.font")); // NOI18N
        numProducts.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        numProducts.setText(resourceMap.getString("numProducts.text")); // NOI18N
        numProducts.setName("numProducts"); // NOI18N

        jLabel25.setFont(resourceMap.getFont("jLabel25.font")); // NOI18N
        jLabel25.setText(resourceMap.getString("jLabel25.text")); // NOI18N
        jLabel25.setName("jLabel25"); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel25)
                    .addComponent(jLabel27))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(amountLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                    .addComponent(numProducts, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel25)
                    .addComponent(numProducts))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel27)
                    .addComponent(amountLabel)))
        );

        goToCheckOut.setFont(resourceMap.getFont("jLabel25.font")); // NOI18N
        goToCheckOut.setText(resourceMap.getString("goToCheckOut.text")); // NOI18N
        goToCheckOut.setToolTipText(resourceMap.getString("goToCheckOut.toolTipText")); // NOI18N
        goToCheckOut.setName("goToCheckOut"); // NOI18N
        goToCheckOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goToCheckOutActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(439, Short.MAX_VALUE)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addComponent(goToCheckOut, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(goToCheckOut, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jScrollPane1.setBackground(resourceMap.getColor("jScrollPane1.background")); // NOI18N
        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setOpaque(false);

        productsPanel.setBackground(resourceMap.getColor("productsPanel.background")); // NOI18N
        productsPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        productsPanel.setName("productsPanel"); // NOI18N
        productsPanel.setOpaque(false);

        javax.swing.GroupLayout productsPanelLayout = new javax.swing.GroupLayout(productsPanel);
        productsPanel.setLayout(productsPanelLayout);
        productsPanelLayout.setHorizontalGroup(
            productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 868, Short.MAX_VALUE)
        );
        productsPanelLayout.setVerticalGroup(
            productsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 547, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(productsPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 911, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 891, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void goToCheckOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goToCheckOutActionPerformed
        nav.goToCheckout();
    }//GEN-LAST:event_goToCheckOutActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel amountLabel;
    private javax.swing.JButton goToCheckOut;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel numProducts;
    private javax.swing.JPanel productsPanel;
    // End of variables declaration//GEN-END:variables

    private void updateText(){
        amountLabel.setText(moneyFormat.format(handler.getShoppingCartTotalCost()));
        numProducts.setText(handler.getShoppingCartItems().size()+" st");
    }
    
    public void shoppingCartChanged() {
        // Remove old
        productsPanel.removeAll();
        List<ShoppingItem> products = handler.getShoppingCartItems();

        // Set layout
        //productsPanel.setLayout(new GridLayout(products.size(),1));
        productsPanel.setLayout(new BoxLayout(productsPanel, BoxLayout.PAGE_AXIS));

        for(int i=0;i<products.size();i++){
            productsPanel.add(new OverviewProduct(products.get(i),i));
            productsPanel.revalidate();
            productsPanel.repaint();
        }

        productsPanel.revalidate();
        productsPanel.repaint();
        updateText();     
    }

    public void favoritesChanged() {
        shoppingCartChanged();
    }

}
